package com.dingup.paike.service.pk.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dingup.api.adapter.user.SysUserAdapter;
import com.dingup.api.model.classInfo.ClassInfo;
import com.dingup.api.model.classInfo.ClassUserInfo;
import com.dingup.paike.dao.PkClassInfoMapper;
import com.dingup.paike.dao.PkUserInfoMapper;
import com.dingup.paike.feign.client.classInfo.ClassInfoFeignClient;
import com.dingup.paike.feign.client.user.UserFeignClient;
import com.dingup.paike.model.pk.PkClassInfo;
import com.dingup.paike.service.pk.PkClassInfoService;
import com.dingup.paike.service.pk.PkLessonInfoService;
import com.dingup.paike.util.EEOUtil;
import com.suyeer.basic.util.BDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

import static com.dingup.paike.util.ConstUtil.*;

/**
 * @author jun 2018/10/24.
 */
@Service
public class PkClassInfoServiceImpl implements PkClassInfoService {

    private final static int CODE_STUDENT = 1;
    private final static int CODE_OBSERVER = 2;
    @Autowired
    private ClassInfoFeignClient classInfoFeignClient;
    @Autowired
    private UserFeignClient userFeignClient;
    @Autowired
    private PkClassInfoMapper pkClassInfoMapper;
    @Autowired
    private PkUserInfoMapper pkUserInfoMapper;
    @Autowired
    private PkLessonInfoService pkLessonInfoService;

    @Override
    public void updateClassInfo(ClassInfo oldClassInfo, ClassInfo newClassInfo) throws Exception {
        updateClassInfoToEEO(oldClassInfo, newClassInfo);
    }

    private void updateClassInfoToEEO(ClassInfo oldClassInfo, ClassInfo newClassInfo) throws Exception {

        PkClassInfo pkClassInfo = pkClassInfoMapper.getByClassId(oldClassInfo.getId());
        if (pkClassInfo != null) {
            List<ClassUserInfo> oldStudentList = oldClassInfo.getStudents();
            List<ClassUserInfo> newStudentList = newClassInfo.getStudents();
            for (ClassUserInfo c : newStudentList) {
                pkLessonInfoService.checkAndSaveEeoUserInfo(c.getUserId());
            }
            delClassStudentToEEO(newStudentList, pkClassInfo.getEeoCourseId(), CODE_OBSERVER);
            delClassStudentToEEO(oldStudentList, pkClassInfo.getEeoCourseId(), CODE_STUDENT);
            addClassStudentToEEO(newStudentList, pkClassInfo.getEeoCourseId());
            EEOUtil.editCourse(newClassInfo.getClassName(), DEFAULT_FOLDER_ID,
                    BDateUtil.getTimestampString(newClassInfo.getEndTime()), pkClassInfo.getEeoCourseId());
        }

    }

    @Override
    public Integer addClassAndSyncEEO(ClassInfo classInfo) throws Exception {
        Integer eeoCourseId = EEOUtil.addCourse(classInfo.getClassName(), DEFAULT_FOLDER_ID,
                BDateUtil.getTimestampString(classInfo.getEndTime()));
        addClassStudentToEEO(classInfo.getStudents(), eeoCourseId);
        PkClassInfo pkClassInfo = new PkClassInfo();
        pkClassInfo.setClassId(classInfo.getId());
        pkClassInfo.setClassName(classInfo.getClassName());
        pkClassInfo.setEeoCourseId(eeoCourseId);
        pkClassInfo.setFolderId(0);
        pkClassInfo.setIfDelete(false);
        pkClassInfoMapper.add(pkClassInfo);
        return eeoCourseId;
    }

    @Override
    public void deleteClassAndSyncEEO(Integer classId) throws Exception {
        PkClassInfo pkClassInfo = pkClassInfoMapper.getByClassId(classId);
        if (pkClassInfo != null) {
            EEOUtil.delCourse(pkClassInfo.getEeoCourseId());
        }
        pkLessonInfoService.deletePkLessonByClass(classId);
    }

    /**
     * EEO班级添加学生
     *
     * @param classUserInfoList List<ClassUserInfo>
     * @param eeoCourseId       Integer
     * @throws Exception Exception
     */
    private void addClassStudentToEEO(List<ClassUserInfo> classUserInfoList, Integer eeoCourseId) throws Exception {
        JSONArray studentArr = new JSONArray();
        if (!CollectionUtils.isEmpty(classUserInfoList)) {
            for (ClassUserInfo classUserInfo : classUserInfoList) {
                SysUserAdapter sysUserAdapter = userFeignClient.getUserByUserId(classUserInfo.getUserId());
                String telephone = getTelephone(sysUserAdapter);
                JSONObject studentObj = new JSONObject();
                studentObj.put(KEY_ACCOUNT, telephone);
                studentObj.put(KEY_NAME, sysUserAdapter.getUserName());
                studentArr.add(studentObj);
            }
            EEOUtil.addCourseStudentMultiple(studentArr, eeoCourseId, CODE_STUDENT);
        }
    }

    private void delClassStudentToEEO(List<ClassUserInfo> classUserInfoList, Integer eeoCourseId, int identity) throws Exception {
        List<String> studentPhoneNumList = new ArrayList<>();
        for (ClassUserInfo classUserInfo : classUserInfoList) {
            SysUserAdapter sysUserAdapter = userFeignClient.getUserByUserId(classUserInfo.getUserId());
            String telephone = getTelephone(sysUserAdapter);
            studentPhoneNumList.add(telephone);
        }
        EEOUtil.deleteCourseStudentMultiple(eeoCourseId, studentPhoneNumList, identity);
    }

    private String getTelephone(SysUserAdapter sysUserAdapter) {
        String telephone = sysUserAdapter.getMobile();
        if (telephone.startsWith(CHINA_CODE)) {
            telephone = sysUserAdapter.getUserAccount();
        }
        return telephone;
    }

}
